﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
namespace Users_Management
{
    public partial class UsersManagement : Form
    {
       
     
        public UsersManagement()
        {
         
            InitializeComponent();

        }
        private int currentNumber = 2;
        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.ListViewItemSorter = new ListViewItemComparer();
        }
        private void add_Click(object sender, EventArgs e)
        {
            int previousNumber = currentNumber++;

            String EnterNameValue = tBnote.Text;
            String ActorValue = tBActor.Text;
            String PasswordValue = tBPassword.Text;
            String PartValue = tBPart.Text;

            ListViewItem newItem = new ListViewItem(previousNumber.ToString());
            newItem.SubItems.Add(EnterNameValue);
            newItem.SubItems.Add(ActorValue);
            newItem.SubItems.Add(PasswordValue);
            newItem.SubItems.Add(PartValue);

            listView1.Items.Add(newItem);

            tBnote.Text = string.Empty;
            tBActor.Text = string.Empty;
            tBPassword.Text = string.Empty;
            tBPart.Text = string.Empty;

            listView1.Sort();
        }
        // ListViewItemComparer 类在同一个命名空间中
        public class ListViewItemComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                return int.Parse(((ListViewItem)x).SubItems[0].Text).CompareTo(int.Parse(((ListViewItem)y).SubItems[0].Text));
            }
        }

        //允许本类可以被序列化
        [Serializable]
         class MyItem
        {
            public string EnterNameValue { get; set; }
            public string ActorValue { get; set; }
            public string PasswordValue { get; set; }
            public string PartValue { get; set; }
        }
       
        private void PreServe_Click(object sender, EventArgs e)
        {
            MyItem myItem = new MyItem()
            {
                EnterNameValue = this.tBnote.Text.Trim(),
                ActorValue = this.tBActor.Text.Trim(),
                PasswordValue = this.tBPassword.Text.Trim(),
                PartValue = this.tBPart.Text.Trim()
            };
            SaveMyItem(myItem);
        }
        //对象到文本文件中的方法
        private void SaveMyItem(MyItem myItem)
        {
            //保存对象到文本文件中
            FileStream fs = new FileStream("myItem.obj", FileMode.Create);
            //创建写入器
            StreamWriter sw = new StreamWriter(fs);
            //逐行写入数据
            sw.WriteLine(myItem.EnterNameValue);
            sw.WriteLine(myItem.ActorValue);
            sw.WriteLine(myItem.PasswordValue);
            sw.WriteLine(myItem.PartValue);
            MessageBox.Show("数据保存成功", "提示");
            sw.Close();  //关闭写入器
            fs.Close();  //关闭数据流
        }
        private void btnSerialize_Click(object sender, EventArgs e)
        {
            //从控件中读取数据并封装成对象
            MyItem myItem = new MyItem()
            {
                EnterNameValue = this.tBnote.Text.Trim(),
                ActorValue = this.tBActor.Text.Trim(),
                PasswordValue = this.tBPassword.Text.Trim(),
                PartValue = this.tBPart.Text.Trim()
            };
            //保存对象到文本文件中(序列化)
            FileStream fs = new FileStream("myItem.obj", FileMode.Create);
            //创建二进制格式化器
            BinaryFormatter bf = new BinaryFormatter();
            //调用序列化方法
            bf.Serialize(fs, myItem);
            MessageBox.Show("数据保存成功", "提示");
            //关闭数据流
            fs.Close();
        }

        private void upActor_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                // 获取选中的行
                ListViewItem selectedItem = listView1.SelectedItems[0];

                

                if (selectedItem != null)
                {
                    
                    selectedItem.SubItems[2].Text = tBActor.Text;
                    

                    tBnote.Text = "";
                    tBActor.Text = "";
                    tBPassword.Text = "";
                    tBPart.Text = "";
                }
            }


            else
            {
                MessageBox.Show("请先选择一行数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
          
            
        }

        private void updatePasseord_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                // 获取选中的行
                ListViewItem selectedItem = listView1.SelectedItems[0];



                if (selectedItem != null)
                {
                   
                    selectedItem.SubItems[3].Text = tBPassword.Text;
                 

                    tBnote.Text = "";
                    tBActor.Text = "";
                    tBPassword.Text = "";
                    tBPart.Text = "";
                }
            }


            else
            {
                MessageBox.Show("请先选择一行数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void updatePart_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                // 获取选中的行
                ListViewItem selectedItem = listView1.SelectedItems[0];



                if (selectedItem != null)
                {
                   
                    selectedItem.SubItems[4].Text = tBPart.Text;

                    tBnote.Text = "";
                    tBActor.Text = "";
                    tBPassword.Text = "";
                    tBPart.Text = "";
                }
            }


            else
            {
                MessageBox.Show("请先选择一行数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void delete_Click(object sender, EventArgs e)
        {
            int Index = 0;
            if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
            {
                Index = this.listView1.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0       
                listView1.Items[Index].Remove();
            }
            MessageBox.Show("数据删除成功", "提示");
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                // 获取选中的行
                ListViewItem selectedItem = listView1.SelectedItems[0];

                // 获取行中的每列值
                string column1Value = selectedItem.SubItems[1].Text; // 假设第一列的值需要传递给 TextBox
                string column2Value = selectedItem.SubItems[2].Text; // 假设第二列的值需要传递给 TextBox
                string column3Value = selectedItem.SubItems[3].Text;
                string column4Value = selectedItem.SubItems[4].Text;// ... 获取其他列的值

                // 将值传递给 TextBox
                tBnote.Text = column1Value;
                tBActor.Text = column2Value;
                tBPassword.Text = column3Value;
                tBPart.Text = column4Value;
                // ... 设置其他 TextBox 的值
            }
            else
            {
                MessageBox.Show("请先选择一行数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
